#include "StdAfx.h"
#include "Utility.h"

Utility::Utility(void)
{
}


Utility::~Utility(void)
{
}

vector<string> Utility::split(const string &s, char delim) {
	vector<string> elems;
	return split(s, delim,elems);
}

vector<string> &Utility::split(const string &s, char delim, vector<string> &elems) {
	stringstream ss(s);
	string item;
	while(getline(ss, item, delim)) {
		elems.push_back(item);
	}
	return elems;
}

bool Utility::startsWith(const string& haystack, const string& needle) {
    return needle.length() <= haystack.length()  && equal(needle.begin(), needle.end(), haystack.begin());
}

bool Utility::endsWith(const string& haystack, const string& needle) {
	return needle.length() <= haystack.length()  &&  (0 == haystack.compare (haystack.length() - needle.length(), needle.length(), needle));
}

bool Utility::contains(const string& haystack, const string& needle) {
	return (haystack.find(needle) != string::npos);
}

string &Utility::ltrim(string &s) {
	s.erase(s.begin(), find_if(s.begin(), s.end(), not1(ptr_fun<int, int>(isspace))));
	return s;
}
string &Utility::rtrim(string &s) {
	s.erase(find_if(s.rbegin(), s.rend(), not1(ptr_fun<int, int>(isspace))).base(), s.end());
	return s;
}
string &Utility::trim(string &s) {
	return ltrim(rtrim(s));
}

string &Utility::removeChars(string &s, char chars[]) {
	for (unsigned int i = 0; i < strlen(chars); ++i)
	{
		s.erase (remove(s.begin(), s.end(), chars[i]), s.end());
	}
	return s;
}